package com.example.pt.task;

import com.example.pt.dto.UserTransferStatsDTO;
import com.example.pt.entity.User;
import com.example.pt.repository.UserRepositories;
import com.example.pt.service.UserService;
import com.example.pt.service.impl.UserTransferStatsServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.sql.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;

@Component
public class UserAssessmentTask {
    private static final Logger logger = LoggerFactory.getLogger(UserAssessmentTask.class);

    @Autowired
    private UserRepositories userRepository;

    @Autowired
    private UserService userService;

    @Autowired
    private UserTransferStatsServiceImpl userTransferStatsService;

    @Scheduled(cron = "0 31 22 * * *")
    public void checkUserInfo() {
        // 检测连续60天不登陆的用户
        LocalDate sixtyDaysAgo = LocalDate.now().minus(60, ChronoUnit.DAYS);
        List<User> inactiveUsers = userRepository.findByLastTimeBefore(Date.valueOf(sixtyDaysAgo));
        // 封号处理
        for(User user : inactiveUsers) {
            if(userService.banUsers(user.getUid())){
                logger.info("用户{}" + user.getUid() + "因连续60天未登录被封禁！");
            }
            else logger.info(user.getUid() + "连续60天未登录的封禁出错！");

        }


        // 检测新手考核未完成任务的用户
        LocalDate oneMonthAgo = LocalDate.now().minusMonths(1);
        List<User> registerUsers = userRepository.findByRegisterDateBefore(Date.valueOf(oneMonthAgo));
        for (User user : registerUsers) {
            UserTransferStatsDTO userDto = userTransferStatsService.getUserTotalTransferStats(user.getUid());
            if(userDto.getTotalDownloaded() < 50 && userDto.getTotalUploaded() < 30){
                //封号处理
                if(userService.banUsers(user.getUid())){
                    logger.info("用户{}" + user.getUid() + "因未通过新用户考核而被封禁！");
                }
                else logger.info(user.getUid() + "新用户考核的封禁出错！");
            }
        }


        LocalDate sevenDayAgo = LocalDate.now().minus(7, ChronoUnit.DAYS);

        //查询状态为2并且Buffer_day到目前为止超过7天的
        List<User> status2Users = userRepository.findByStatusAndBufferDayBefore(2,Date.valueOf(sevenDayAgo));
        for(User user : status2Users) {
            userService.banUsers(user.getUid());
        }

        // 检测分享率相关逻辑，需要结合业务获取用户下载量，再根据下载量区间和对应分享率要求判断，添加处理逻辑
        //查询状态不为2的全体user
        List<User> shareUsers = userRepository.findByStatusNot(2);
        for(User user : shareUsers) {
            UserTransferStatsDTO userDto = userTransferStatsService.getUserTotalTransferStats(user.getUid());

            Double ratio = userDto.getRatio();
            Long totalDownloaded = userDto.getTotalDownloaded();

            if(totalDownloaded > 0 && totalDownloaded <= 50 && ratio < 0.5){
                userService.status2AndBufferDayUser(user);
            }
            else if(totalDownloaded > 50 && totalDownloaded <= 100 && ratio < 0.6){
                userService.status2AndBufferDayUser(user);
            }
            else if(totalDownloaded > 100 && totalDownloaded <= 500 && ratio < 0.8){
                userService.status2AndBufferDayUser(user);
            }
            else if(totalDownloaded > 500 && totalDownloaded <= 2000 && ratio < 1.1){
                userService.status2AndBufferDayUser(user);
            }
            else if(totalDownloaded > 2000 && ratio < 1.4){
                userService.status2AndBufferDayUser(user);
            }
        }

    }
}
